package com.css.dzj.dzjportal.modules.promulgate.controller;

import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.modules.promulgate.entity.ProEntity;
import com.css.dzj.dzjportal.modules.promulgate.service.ProService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/promulgate")
public class ProController {
    @Value("${AP_URL}")
    private String apUrl;
    @Autowired
    private ProService proService;
    /**
     * 按条件查询
     */
    @GetMapping("/titleList")
    public R titleList(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                       @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                       @RequestParam(name = "title" ,required = false) String title,
                       @RequestParam(name = "publicType" ,required = false) String publicType,
                       @RequestParam(name = "isPublic" ,required = false) String isPublic){
        try {
            if(StringUtils.isEmpty(title)) {
                return R.error("条件信息不能为空");
            }
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<ProEntity> tlist =  proService.titleList(pageRequest,title,publicType,pageSize,pageNo,isPublic);
            return R.ok().put("data",tlist);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * 查询所有
     */
    @GetMapping("/allList")
    public R allList(@RequestParam(name = "pageSize" ,required = false)Integer pageSize,
                     @RequestParam(name = "pageNo" ,required = false)Integer pageNo,
                     @RequestParam(name = "publicType" ,required = false) String publicType,
                     @RequestParam(name = "isPublic" ,required = false) String isPublic){
        try {
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            QueryResults<ProEntity> tlist =  proService.allList(pageRequest,publicType,pageSize,pageNo,isPublic);
            return R.ok().put("data",tlist);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    /**
     * 查询详情
     */
    @GetMapping("/selectOne")
    public R allList(String id,String from){
        try {
            ProEntity pro =  proService.selectOne(id,from);
            return R.ok().put("data",pro);
        } catch(Exception e) {
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
}
